package com.unknownman.block.controller.admin.inner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unknownman.block.entity.TContractConfig;
import com.unknownman.block.entity.TContractLogs;
import com.unknownman.block.service.TContractConfigService;
import com.unknownman.block.service.TContractLogsService;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.cloud.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/inner/call")
public class InnerCallController {

    @Autowired
    private TContractLogsService contractLogsService;
    @Autowired
    private TContractConfigService contractConfigService;

    @ApiOperation("获取交易记录")
    @RequestMapping(method = RequestMethod.GET, value = "/getTranLogList")
    public List<TContractLogs> getTranLogListByContractAddress(
        @RequestParam(value = "contractAddress", required = false) String contractAddress,
        @RequestParam(value = "firstTopic", required = false) String firstTopic,
        @RequestParam(value = "startId", required = false) Long startId) throws BusinessException {
        QueryWrapper<TContractLogs> where = new QueryWrapper<>();
        where.eq(!StringUtils.isEmpty(contractAddress), "contract_address", contractAddress);
        where.eq(!StringUtils.isEmpty(firstTopic), "first_topic", firstTopic);
        where.ge(!StringUtils.isEmpty(startId), "contract_log_id", startId);
        return contractLogsService.list(where);
    }

    @ApiOperation("根据符号获取合约配置")
    @RequestMapping(method = RequestMethod.GET, value = "/getBySymbol/{symbol}")
    public TContractConfig getBySymbol(@PathVariable("symbol") String symbol){
        return contractConfigService.getBySymbol(symbol);
    }
}
